package com.googlecode.imule.apps;

import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import javax.imageio.ImageIO;
import javax.swing.*;
import com.apple.dnssd.DNSSDException;

/**
 * Speaker - plays the music
 * 
 * @author Owen Phelan, Mike Solomon
 */

public class Speaker extends Canvas implements KeyListener {
	public boolean state = true;
	ImageIcon image;

	public JFrame x;
	public JPanel y;
	public BufferedImage on;
	public BufferedImage off;
	BufferStrategy bs;
	public int volume = 0;
	X speaker;
	String name;
	Scanner scanner = new Scanner(System.in);
	String input = "";

	public Speaker() throws IOException {
		x = new JFrame();
		y = (JPanel) x.getContentPane();
		setBounds(0, 0, 400, 400);
		x.setPreferredSize(new Dimension(400, 400));
		x.setLayout(null);
		x.add(this);
		x.setBounds(50, 50, 400, 400);
		x.setVisible(true);
		addKeyListener(this);

		createBufferStrategy(2);
		bs = getBufferStrategy();

		File in = new File("resources/images/speakerOff.jpg");
		off = (BufferedImage) ImageIO.read(in);
		in = new File("resources/images/speakerOn.jpg");
		on = (BufferedImage) ImageIO.read(in);
		x.setDefaultCloseOperation(1);
	}

	public void paintSpeaker() {
		Graphics2D g = (Graphics2D) bs.getDrawGraphics();
		if (state == false) {
			g.drawImage(on, 0, 0, this);
		} else {
			g.drawImage(off, 0, 0, this);
		}
		bs.show();
	}

	public boolean returnState() {
		return state;
	}

	/**
	 * Set the state of the device
	 * 
	 * @param state
	 */
	public void setState(Boolean state) {
		this.state = state;
	}
	
	/* Handles UDPCommunicationX requirements for this object
	 * With command line interface
	 */
	public void openComms() throws IOException, DNSSDException {
		Scanner scanner = new Scanner(System.in);

		String name = "speaker";
		speaker = new X(name);
		speaker.setManifest("resources/manifests/speaker.xml");
		System.out.println(speaker.getManifest());
		String input = "";

		while (!input.equalsIgnoreCase("close")) {
			input = scanner.nextLine();
			if (input.charAt(0) == '@') {
				String[] split = input.split("\\s");
				String serviceName = split[0].substring(1, split[0].length());
				if (split[1].charAt(0) == '@') {
					speaker.sendAction(serviceName);
				} else {
					String message = "";
					for (int i = 1; i < split.length; i++) {
						message += split[i] + " ";
					}
					speaker.send(message, serviceName);
				}
			} else {
				speaker.broadcast(input);
			}
		}
		System.out.println("closing");

		System.exit(0);
	}

	/**
	 * main
	 * Initiates image, with listener, and initiates communications protocol
	 * @throws IOException
	 * @throws DNSSDException
	 */
	public static void main(String[] args) throws IOException, DNSSDException {
		
		Speaker s = new Speaker();
		s.paintSpeaker();
		s.openComms();
	}

	/* Exit system? */
	public void kill() {
		System.exit(0);
	}

	/* Used to swap images with keystroke */
	public void keyPressed(KeyEvent e) {
		switch (e.getKeyCode()) {
		case KeyEvent.VK_O:
			state = !state;
			paintSpeaker();
			break;
		}
	}

	public void keyReleased(KeyEvent e) {
		// Unused methods, required by keylistener implementation
	}

	public void keyTyped(KeyEvent e) {
		// Unused methods, required by keylistener implementation
	}
}
